package com.clt.ess.service.impl;

import com.clt.ess.base.Constant;
import com.clt.ess.crypt.ExDataStru;
import com.clt.ess.dao.ISealApplyDao;
import com.clt.ess.dao.ISealDao;
import com.clt.ess.entity.*;
import com.clt.ess.gjzw.domain.FilePackage;
import com.clt.ess.gjzw.domain.SealData;
import com.clt.ess.gjzw.domain.TaskNumData;
import com.clt.ess.gjzw.domain.uploadRecordInfos.UploadRecordInfosRequestData;
import com.clt.ess.multica.crypt.MuticaCrypt;
import com.clt.ess.multica.crypt.MuticaCryptException;
import com.clt.ess.service.*;
import com.clt.ess.entity.SysVerify;
import com.clt.ess.utils.MakeAsn1Seal;
import com.clt.ess.utils.dateUtil;
import com.clt.ess.multica.crypt.VerifyServerAuthority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import static com.clt.ess.utils.Base64Utils.*;
import static com.clt.ess.utils.StringUtils.*;
import static com.clt.ess.utils.dateUtil.*;
import static java.lang.Thread.sleep;

@Service
public class SealServiceImpl implements ISealService {
    @Autowired
    private ISealDao sealDao;
    @Autowired
    private ISealApplyDao sealApplyDao;
    @Autowired
    private ICertificateService certificateService;
    @Autowired
    private ISysVerifyService sysVerifyService;
    @Autowired
    private IUnitService unitService;
    @Autowired
    private IGjzwService gjzwService;
    @Autowired
    private ISealImgService sealImgService;
    @Autowired
    private IUserService userService;
    @Override
    public List<SealType> findSealType(SealType sealType) {
        return sealDao.findSealType(sealType);
    }
    @Override
    public boolean addSealApply(SealApply sealApply) {

        //加密处理
        int result = sealApplyDao.addSealApply(EncryptSealApply(sealApply));

//        DecryptSealApply(sealApply);

        return result == 1;
    }
    @Override
    public List<SealApply> findSealApply(SealApply sealApply) {
        List<SealApply> sealApplies = sealApplyDao.findSealApply(sealApply);
        for (SealApply sealApply1 :sealApplies){
            sealApply1 = DecryptSealApply(sealApply1);

            if (sealApply1.getApplyUserId()!=null){
                sealApply1.setApplyUser(userService.findUserById(sealApply1.getApplyUserId()));
            }
            if (sealApply1.getReviewUserId()!=null){
                sealApply1.setReviewUser(userService.findUserById(sealApply1.getReviewUserId()));
            }
            if (sealApply1.getMakeUserId()!=null){
                sealApply1.setMakeUser(userService.findUserById(sealApply1.getMakeUserId()));
            }
            sealApply1.setUnit(unitService.findUnitById(sealApply1.getUnitId()));
            sealApply1.setMakeUnit(unitService.findUnitById(sealApply1.getMake_unit_id()));
            sealApply1.setCertificate(certificateService.findCertificateById(sealApply1.getCertificateId()));
            sealApply1.setSealImg(sealImgService.findSealImgById(sealApply1.getSealImgId()));


        }
        return sealApplies;
    }
    @Override
    public boolean delSealApplyById(SealApply sealApply) {
        int result = sealApplyDao.delSealApplyById(sealApply);
        return result == 1;
    }

    @Override
    public boolean updateSealApply(SealApply sealApply) {
        int result = sealApplyDao.updateSealApply(EncryptSealApply(sealApply));
        DecryptSealApply(sealApply);
        return result == 1;
    }

    @Override
    public List<Seal> findSealList(Seal seal) {
        List<Seal> sealList = sealDao.findSealList(seal);
        for (Seal seal1 :sealList){
            seal1 = DecryptSeal(seal1);


            seal1.setUnit(unitService.findUnitById(seal1.getUnitId()));
            seal1.setMake_unit(unitService.findUnitById(seal1.getMake_unit_id()));
            seal1.setCertificate(certificateService.findCertificateById(seal1.getCerId()));
            seal1.setSealImg(sealImgService.findSealImgById(seal1.getSealImgId()));
        }
        return sealList;
    }

    @Override
    public boolean addSeal(Seal newSeal) {
        //印章更新
        int result = sealDao.addSeal(EncryptSeal(newSeal));
        System.out.println("ess7&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
        DecryptSeal(newSeal);
        return result == 1;
    }

    @Override
    public void delSeal(Seal seal) {
        int result = sealDao.delSeal(seal);
    }

    @Override
    public List<Seal> findSealListByIdNums(List<String> personIdNums) {

        //此方法失效
        return sealDao.findSealListByIdNums(personIdNums);
    }

    @Override
    public boolean updateSeal(Seal seal_up) {
        System.out.println("3##############################################################################################");
        int result = sealDao.updateSeal(EncryptSeal(seal_up));
        DecryptSeal(seal_up);
        return result == 1;
    }

    @Override
    public boolean createSealApplyInfo(SealApply sealApply, SealImg sealImg, Certificate c) {
        return false;
    }

    @Override
    public SealApply findSealApplyById(String sealApplyId) {
        SealApply sealApply = DecryptSealApply(sealApplyDao.findSealApplyById(sealApplyId));

        sealApply.setUnit(unitService.findUnitById(sealApply.getUnitId()));
        sealApply.setMakeUnit(unitService.findUnitById(sealApply.getMake_unit_id()));
        sealApply.setCertificate(certificateService.findCertificateById(sealApply.getCertificateId()));
        sealApply.setSealImg(sealImgService.findSealImgById(sealApply.getSealImgId()));

        return sealApply;
    }
    @Override
    public Seal findSealByKeyId(String keyId) {
        System.out.println("##############################################################################################");
        System.out.println(keyId+"------------keyid");
        System.out.println("##############################################################################################");
        Seal seal1 = DecryptSeal(sealDao.findSealByKeyId(getEncryptString(keyId)));
        if (seal1==null){
            return null;
        }
        if (seal1.getUnitId()!=null){
            seal1.setUnit(unitService.findUnitById(seal1.getUnitId()));
        }
        if (seal1.getMake_unit_id()!=null){
            seal1.setMake_unit(unitService.findUnitById(seal1.getMake_unit_id()));
        }
        if (seal1.getCerId()!=null){
            seal1.setCertificate(certificateService.findCertificateById(seal1.getCerId()));
        }
        if (seal1.getSealImgId()!=null){
            seal1.setSealImg(sealImgService.findSealImgById(seal1.getSealImgId()));
        }
        return seal1;
    }

    @Override
    public Seal findSealById(String sealId) {
        Seal seal1 = DecryptSeal(sealDao.findSealById(sealId));
        seal1.setUnit(unitService.findUnitById(seal1.getUnitId()));
        seal1.setMake_unit(unitService.findUnitById(seal1.getMake_unit_id()));
        seal1.setCertificate(certificateService.findCertificateById(seal1.getCerId()));
        seal1.setSealImg(sealImgService.findSealImgById(seal1.getSealImgId()));

        return seal1;
    }

    @Override
    public int findSealCountByUnitId(String unitId) {
        return sealDao.findSealCountByUnitId(unitId);
    }


    @Override
    public int isAddSeal(String unitId) {
        // 判断当前配置的服务器是否有添加人员的剩余数量
        // 获取当前登录用户的单位Id
        // 独立单位Id,可能是当前登录人的单位id，也可能是一级单位id
        String independentId = "";
        // 先根据当前印章的单位id查询,若查询不到,则查询独立单位id（一级单位）,并根据独立单位id查
        SysVerify sysVerify = sysVerifyService.findSysVerifyById(unitId);
        // 先将其设置为当前登录人的所属单位Id
        independentId = unitId;
        if(sysVerify == null || sysVerify.getVerifyResult() == null){
            Unit unit = unitService.findUnitById(unitId);
            if(unit == null){
                return 1;
            }
            // 查询一级单位的Id
            Unit topUnit = unitService.findTopUnit(unitId);
            // 如果通过当前登录人没有查询到验证信息,则通过独立单位id查询
            independentId = topUnit.getUnitId();
            sysVerify = sysVerifyService.findSysVerifyById(topUnit.getUnitId());
        }
        if(sysVerify == null || sysVerify.getVerifyResult() == null){
            //无权限
			return 1;
        }
        int verifyStatus = VerifyServerAuthority.GetServerAuthorityVerifyStatus(dateUtil.getCurrentTimeToMinuteNoDelimiter(), sysVerify.getVerifyResult());
        if(verifyStatus == 1 || verifyStatus == 3){
			//验证失败
			return 2;
        }else if(verifyStatus == 2){
            int iSealMaxCount = sysVerify.getUkSealJurSize() + sysVerify.getMobileSealJurSize() + sysVerify.getUkHwJurSize() + sysVerify.getMobileHwJurSize();
            String verifyServerAuth = VerifyServerAuthority.VerifyServerAuth(sysVerify.getUnitName(), sysVerify.getServerIp(), iSealMaxCount, sysVerify.getJurProductCode(), sysVerify.getDueTime(), sysVerify.getSignValue());
            if(verifyServerAuth == null || "".equals(verifyServerAuth)){
                // 验证失败
                return 2;
            }else{
                // 验证成功,将返回值写入查询出的sysVerify的验证结果字段中
                sysVerify.setVerifyResult(verifyServerAuth);
                // 查询当前数据库中有效状态下的印章数量
                Integer sealSize = findSealCountByUnitId(independentId);
                if(sealSize == null){
                    //验证失败
                    return 2;
                }
                if(sealSize == sysVerify.getUkSealJurSize()){
                    //false
                }

                sysVerify.setIndependentId(independentId);

                // 执行更新操作
                Integer size = sysVerifyService.updateVerifyResultByIndepdId(sysVerify);
                return 3;
            }
        }
        return 0;
    }

    @Override
    public TaskNumData recordSeal(Certificate certificate,SealApply sealApply)  {


        String encCert = certificate.getPfxBase64();
        String signCert = certificate.getCerBase64();

        List<UploadRecordInfosRequestData> s = new ArrayList<>();
        UploadRecordInfosRequestData uploadRecordInfosRequestData = new UploadRecordInfosRequestData();
        //加密证书
        uploadRecordInfosRequestData.setSealEncCert(encCert);
        //签名证书
        uploadRecordInfosRequestData.setSealSignCert(signCert);
        //行政区划代码
        uploadRecordInfosRequestData.setDistrictCode(sealApply.getUnit().getArea_number());
        //印章数据
        SealData sealData = new SealData();
        //印章名称
        sealData.setYzmc(sealApply.getSealName());
        //印章编码
        sealData.setYzbm(sealApply.getSeal_code());
        //印章制作单位编码
        sealData.setYzzzdwbm(sealApply.getMake_unit_id());
        //印章类型代码
        sealData.setYzlxdm(sealApply.getSealTypeId());
        //经办人姓名
        sealData.setJbr_xm(sealApply.getJbr_name());
        //经办人证件类型
        sealData.setJbr_zjlx(sealApply.getJbr_card_type());
        //经办人证件号码
        sealData.setJbr_zjhm(sealApply.getJbr_card_data());
        //制作日期
        sealData.setZzrq(sealApply.getMakeTime());
        //印模类型
        sealData.setYmlx("png");
        //印模数据
        sealData.setYmsj(sealApply.getSealImg().getSealImgGifBase64());
        //印章使用单位_单位名称
        sealData.setYzsydw_dwmc(sealApply.getUnit().getUnitName());
        //印章制作单位_单位名称
        sealData.setYzzzdw_dwmc(sealApply.getMakeUnit().getUnitName());
        //印章使用单位——统一社会信用代码
        sealData.setYzsydw_tyshxydm(sealApply.getUnit().getCredit_code());

        uploadRecordInfosRequestData.setSealData(sealData);
        s.add(uploadRecordInfosRequestData);
        TaskNumData taskNumData = gjzwService.uploadRecordInfosGetTaskId(Constant.applicantCert,s);
        if (taskNumData==null){
            return null;
        }

        return  taskNumData;

    }

    @Override
    public FilePackage checkResult(TaskNumData taskNumData) {
        //下载
        try {
            sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        FilePackage filePackage = gjzwService.uploadRecordInfoCheckResult(taskNumData);

        if (filePackage==null){
            try {
                sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            filePackage = gjzwService.uploadRecordInfoCheckResult(taskNumData);
            return filePackage;
        }
        return filePackage;
    }

    @Override
    public boolean sealApplyToSeal(SealApply sealApply) {

        //查找是否有相同keyid 的数据，如果有删除
        Seal seal1 = findSealByKeyId(sealApply.getKeyid());
        if (seal1!=null){
            delSeal(seal1);
        }
        System.out.println("ess4&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
        //生成印章信息
        Seal seal =new Seal();
        seal.setSealId(sealApply.getSealId());
        seal.setSeal_code(sealApply.getSeal_code());
        seal.setSealName(sealApply.getSealName());
        seal.setSealImgId(sealApply.getSealImgId());
        seal.setCerId(sealApply.getCertificateId());
        seal.setInputUserId(sealApply.getMakeUserId());
        seal.setInputTime(getDate());
        seal.setUnitId(sealApply.getUnitId());
        seal.setJbr_name(sealApply.getJbr_name());
        seal.setJbr_card_type(sealApply.getJbr_card_type());
        seal.setJbr_card_data(sealApply.getJbr_card_data());
        seal.setMake_unit_id(sealApply.getMake_unit_id());
        seal.setSealTypeId(sealApply.getSealTypeId());
        seal.setSealStartTime(sealApply.getSealStartTime());
        seal.setSealEndTime(sealApply.getSealEndTime());
        seal.setDevice_code(sealApply.getDevice_code());
        seal.setSealState(1);
        seal.setKeyid(sealApply.getKeyid());
        seal.setAppSymKeyEnc(sealApply.getAppSymKeyEnc());
        seal.setDataSymKeyEnc(sealApply.getDataSymKeyEnc());
        seal.setEncFile(sealApply.getEncFile());
        seal.setTaskId(sealApply.getTaskId());
        seal.setCertificate(seal.getCertificate());


        Certificate certificate = new Certificate();
        certificate.setCertificateId(seal.getCerId());
        certificate.setState(1);
        List<Certificate> certificateList = certificateService.findCertificate(certificate);

        System.out.println("ess5&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");

        String sCert = Constant.applicantCert;
        Vector<String> vCert = new Vector<String>();
        vCert.addElement(certificateList.get(0).getCerBase64());
        byte[] bPic = ESSGetBase64Decode(sealApply.getSealImg().getSealImgGifBase64());
        Vector<ExDataStru> exDatas = new Vector<ExDataStru>();
        ExDataStru stru = new ExDataStru();
        stru.oid = "1.2.156.112600.7.1";
        stru.blOption = true;
        stru.sData = sealApply.getUnit().getCredit_code()+sealApply.getUnit().getUnitName();
        exDatas.addElement(stru);

        byte[] bMakerCert = ESSGetBase64Decode(sCert);

        byte[] value = new byte[0];
        try {
            value = MakeAsn1Seal.CreateSeal(
                    "ES",
                    4,
                    "GuoJiaYanCaoZhuanMaiJu",
                    sealApply.getSeal_code(),
                    Integer.parseInt(sealApply.getSealTypeId()),
                    sealApply.getSealName(),
                    1,
                    vCert,
                    DateToStringShort(sealApply.getMakeTime())+"Z",
                    DateToStringShort(sealApply.getSealStartTime())+"Z",
                    DateToStringShort((sealApply.getSealEndTime()))+"Z",
                    sealApply.getSealImg().getSealImageType(),
                    bPic,
                    sealApply.getSealImg().getSealImageW(),
                    sealApply.getSealImg().getSealImageH(),
                    exDatas,
                    bMakerCert,
                    "1.2.156.10197.1.501"
            );
        } catch (IOException e) {
            e.printStackTrace();
        }
        //uk信息文件
        readBin2Image(value,"/data/"+"test/"+seal.getSeal_code()+".clt");

        seal.setUkinfo(ESSGetBase64Encode(value));
        String signature = MakeAsn1Seal.getbSignVal(
                "ES",
                4,
                "GuoJiaYanCaoZhuanMaiJu",
                sealApply.getSeal_code(),
                Integer.parseInt(sealApply.getSealTypeId()),
                sealApply.getSealName(),
                1,
                vCert,
                DateToStringShort(sealApply.getMakeTime())+"Z",
                DateToStringShort(sealApply.getSealStartTime())+"Z",
                DateToStringShort((sealApply.getSealEndTime()))+"Z",
                sealApply.getSealImg().getSealImageType(),
                bPic,
                sealApply.getSealImg().getSealImageW(),
                sealApply.getSealImg().getSealImageH(),
                exDatas,
                bMakerCert,
                "1.2.156.10197.1.501"
        );

        seal.setSignature(signature);

        System.out.println("ess5&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
        return addSeal(seal);
    }
    @Override
    public boolean updateSealUKInfo(Seal seal) {

        System.out.println("1##############################################################################################");
        String sCert = Constant.applicantCert;
        Vector<String> vCert = new Vector<String>();
        vCert.addElement(seal.getCertificate().getCerBase64());
        byte[] bPic = ESSGetBase64Decode(seal.getSealImg().getSealImgGifBase64());
        Vector<ExDataStru> exDatas = new Vector<ExDataStru>();
        ExDataStru stru = new ExDataStru();
        stru.oid = "1.2.156.112600.7.1";
        stru.blOption = true;
        stru.sData = seal.getUnit().getCredit_code()+seal.getUnit().getUnitName();
        exDatas.addElement(stru);

        byte[] bMakerCert = ESSGetBase64Decode(sCert);

        byte[] value = new byte[0];
        try {
            value = MakeAsn1Seal.CreateSeal(
                    "ES",
                    4,
                    "GuoJiaYanCaoZhuanMaiJu",
                    seal.getSeal_code(),
                    Integer.parseInt(seal.getSealTypeId()),
                    seal.getSealName(),
                    1,
                    vCert,
                    DateToStringShort(seal.getInputTime())+"Z",
                    DateToStringShort(seal.getSealStartTime())+"Z",
                    DateToStringShort((seal.getSealEndTime()))+"Z",
                    seal.getSealImg().getSealImageType(),
                    bPic,
                    seal.getSealImg().getSealImageW(),
                    seal.getSealImg().getSealImageH(),
                    exDatas,
                    bMakerCert,
                    "1.2.156.10197.1.501"
            );
        } catch (IOException e) {
            e.printStackTrace();
        }

        readBin2Image(value,"/data/test/1.a");

        seal.setUkinfo(ESSGetBase64Encode(value));
        String signature = MakeAsn1Seal.getbSignVal(
                "ES",
                4,
                "GuoJiaYanCaoZhuanMaiJu",
                seal.getSeal_code(),
                Integer.parseInt(seal.getSealTypeId()),
                seal.getSealName(),
                1,
                vCert,
                DateToStringShort(seal.getInputTime())+"Z",
                DateToStringShort(seal.getSealStartTime())+"Z",
                DateToStringShort((seal.getSealEndTime()))+"Z",
                seal.getSealImg().getSealImageType(),
                bPic,
                seal.getSealImg().getSealImageW(),
                seal.getSealImg().getSealImageH(),
                exDatas,
                bMakerCert,
                "1.2.156.10197.1.501"
        );

        seal.setSignature(signature);
        System.out.println("2##############################################################################################");
        return updateSeal(seal);
    }

    private SealApply EncryptSealApply(SealApply sealApply){
        if (sealApply==null){
            return null;
        }
        sealApply.setSafeHash("");
        //印章编码
        if (sealApply.getSeal_code()!=null){
            sealApply.setSeal_code(getEncryptString(sealApply.getSeal_code()));
        }
        //印章名称
        if (sealApply.getSealName()!=null){
            sealApply.setSealName(getEncryptString(sealApply.getSealName()));
        }
        //经办人姓名
        if (sealApply.getJbr_name()!=null){
            sealApply.setJbr_name(getEncryptString(sealApply.getJbr_name()));
        }
        //经办人证件号码
        if (sealApply.getJbr_card_data()!=null){
            sealApply.setJbr_card_data(getEncryptString(sealApply.getJbr_card_data()));
        }
        //keyId
        if (sealApply.getKeyid()!=null){
            sealApply.setKeyid(getEncryptString(sealApply.getKeyid()));
        }
        //印章起始时间
        if (sealApply.getSealStartTime()!=null){
            sealApply.setSealStartTime(getEncryptString(sealApply.getSealStartTime()));
        }
        //印章失效时间
        if (sealApply.getSealEndTime()!=null){
            sealApply.setSealEndTime(getEncryptString(sealApply.getSealEndTime()));
        }
        //设备编码
        if (sealApply.getDevice_code()!=null){
            sealApply.setDevice_code(getEncryptString(sealApply.getDevice_code()));
        }
//        //设备编码密文
//        if (sealApply.getDataSymKeyEnc()!=null){
//            sealApply.setDataSymKeyEnc(getEncryptString(sealApply.getDataSymKeyEnc()));
//        }
//        //公安下发 加密文件
//        if (sealApply.getEncFile()!=null){
//            sealApply.setEncFile(getEncryptString(sealApply.getEncFile()));
//        }
//        //公安下发 密钥
//        if (sealApply.getDataSymKeyEnc()!=null){
//            sealApply.setDataSymKeyEnc(getEncryptString(sealApply.getDataSymKeyEnc()));
//        }
//        //公安下发 密钥
//        if (sealApply.getAppSymKeyEnc()!=null){
//            sealApply.setAppSymKeyEnc(getEncryptString(sealApply.getAppSymKeyEnc()));
//        }
        return sealApply;
    }


    private SealApply DecryptSealApply(SealApply sealApply){
        if (sealApply==null){
            return null;
        }
        //印章编码
        if (sealApply.getSeal_code()!=null&&!"".equals(sealApply.getSeal_code())){
            sealApply.setSeal_code(getDecryptString(sealApply.getSeal_code()));
        }
        //印章名称
        if (sealApply.getSealName()!=null&&!"".equals(sealApply.getSealName())){
            sealApply.setSealName(getDecryptString(sealApply.getSealName()));
        }
        //经办人姓名
        if (sealApply.getJbr_name()!=null&&!"".equals(sealApply.getJbr_name())){
            sealApply.setJbr_name(getDecryptString(sealApply.getJbr_name()));
        }
        //经办人证件号码
        if (sealApply.getJbr_card_data()!=null&&!"".equals(sealApply.getJbr_card_data())){
            sealApply.setJbr_card_data(getDecryptString(sealApply.getJbr_card_data()));
        }
        //keyId
        if (sealApply.getKeyid()!=null&&!"".equals(sealApply.getKeyid())){
            sealApply.setKeyid(getDecryptString(sealApply.getKeyid()));
        }
        //印章起始时间
        if (sealApply.getSealStartTime()!=null&&!"".equals(sealApply.getSealStartTime())){
            sealApply.setSealStartTime(getDecryptString(sealApply.getSealStartTime()));
        }
        //印章失效时间
        if (sealApply.getSealEndTime()!=null&&!"".equals(sealApply.getSealEndTime())){
            sealApply.setSealEndTime(getDecryptString(sealApply.getSealEndTime()));
        }
        //设备编码
        if (sealApply.getDevice_code()!=null&&!"".equals(sealApply.getDevice_code())){
            sealApply.setDevice_code(getDecryptString(sealApply.getDevice_code()));
        }
//        //设备编码密文
//        if (sealApply.getDataSymKeyEnc()!=null&&"".equals(sealApply.getDataSymKeyEnc())){
//            sealApply.setDataSymKeyEnc(getDecryptString(sealApply.getDataSymKeyEnc()));
//        }
//        //公安下发 加密文件
//        if (sealApply.getEncFile()!=null&&"".equals(sealApply.getEncFile())){
//            sealApply.setEncFile(getDecryptString(sealApply.getEncFile()));
//        }
//        //公安下发 密钥
//        if (sealApply.getDataSymKeyEnc()!=null){
//            sealApply.setDataSymKeyEnc(getDecryptString(sealApply.getDataSymKeyEnc()));
//        }
//        //公安下发 密钥
//        if (sealApply.getAppSymKeyEnc()!=null){
//            sealApply.setAppSymKeyEnc(getDecryptString(sealApply.getAppSymKeyEnc()));
//        }
      

        return sealApply;
    }
    private Seal EncryptSeal(Seal seal){
        if (seal==null){
            return null;
        }
        //印章编码
        if (seal.getSeal_code()!=null){
            seal.setSeal_code(getEncryptString(seal.getSeal_code()));
        }
        //印章名称
        if (seal.getSealName()!=null){
            seal.setSealName(getEncryptString(seal.getSealName()));
        }
        //经办人姓名
        if (seal.getJbr_name()!=null){
            seal.setJbr_name(getEncryptString(seal.getJbr_name()));
        }
        //经办人证件号码
        if (seal.getJbr_card_data()!=null){
            seal.setJbr_card_data(getEncryptString(seal.getJbr_card_data()));
        }
        //keyId
        if (seal.getKeyid()!=null){
            seal.setKeyid(getEncryptString(seal.getKeyid()));
        }
        //印章起始时间
        if (seal.getSealStartTime()!=null){
            seal.setSealStartTime(getEncryptString(seal.getSealStartTime()));
        }
        //印章失效时间
        if (seal.getSealEndTime()!=null){
            seal.setSealEndTime(getEncryptString(seal.getSealEndTime()));
        }
        //设备编码
        if (seal.getDevice_code()!=null){
            seal.setDevice_code(getEncryptString(seal.getDevice_code()));
        }
        //设备编码密文
        if (seal.getDataSymKeyEnc()!=null){
            seal.setDataSymKeyEnc(getEncryptString(seal.getDataSymKeyEnc()));
        }
        //公安下发 加密文件
//        if (seal.getEncFile()!=null){
//            seal.setEncFile(getEncryptString(seal.getEncFile()));
//        }
        //公安下发 密钥
//        if (seal.getDataSymKeyEnc()!=null){
//            seal.setDataSymKeyEnc(getEncryptString(seal.getDataSymKeyEnc()));
//        }
        //公安下发 密钥
//        if (seal.getAppSymKeyEnc()!=null){
//            seal.setAppSymKeyEnc(getEncryptString(seal.getAppSymKeyEnc()));
//        }
        //UK info
//        if (seal.getUkinfo()!=null){
//            seal.setUkinfo(getEncryptString(seal.getUkinfo()));
//        }
        //签名值
        if (seal.getSignature()!=null){
            seal.setSignature(getEncryptString(seal.getSignature()));
        }
        return seal;
    }

    private Seal DecryptSeal(Seal seal){
        if (seal==null){
            return null;
        }

        //印章编码
        if (seal.getSeal_code()!=null){
            seal.setSeal_code(getDecryptString(seal.getSeal_code()));
        }
        //印章名称
        if (seal.getSealName()!=null){
            seal.setSealName(getDecryptString(seal.getSealName()));
        }
        //经办人姓名
        if (seal.getJbr_name()!=null){
            seal.setJbr_name(getDecryptString(seal.getJbr_name()));
        }
        //经办人证件号码
        if (seal.getJbr_card_data()!=null){
            seal.setJbr_card_data(getDecryptString(seal.getJbr_card_data()));
        }
        //keyId
        if (seal.getKeyid()!=null){
            seal.setKeyid(getDecryptString(seal.getKeyid()));
        }
        //印章起始时间
        if (seal.getSealStartTime()!=null){
            seal.setSealStartTime(getDecryptString(seal.getSealStartTime()));
        }
        //印章失效时间
        if (seal.getSealEndTime()!=null){
            seal.setSealEndTime(getDecryptString(seal.getSealEndTime()));
        }
        //设备编码
        if (seal.getDevice_code()!=null){
            seal.setDevice_code(getDecryptString(seal.getDevice_code()));
        }
        //设备编码密文
        if (seal.getDataSymKeyEnc()!=null){
            seal.setDataSymKeyEnc(getDecryptString(seal.getDataSymKeyEnc()));
        }
        //公安下发 加密文件
//        if (seal.getEncFile()!=null){
//            seal.setEncFile(getDecryptString(seal.getEncFile()));
//        }
        //公安下发 密钥
//        if (seal.getDataSymKeyEnc()!=null){
//            seal.setDataSymKeyEnc(getDecryptString(seal.getDataSymKeyEnc()));
//        }
        //公安下发 密钥
//        if (seal.getAppSymKeyEnc()!=null){
//            seal.setAppSymKeyEnc(getDecryptString(seal.getAppSymKeyEnc()));
//        }
        //UK info
//        if (seal.getUkinfo()!=null){
//            seal.setUkinfo(getDecryptString(seal.getUkinfo()));
//        }
        //签名值
        if (seal.getSignature()!=null){
            seal.setSignature(getDecryptString(seal.getSignature()));
        }

        return seal;
    }

}
